# PURPOSE: Construct Study 3 plots for appendices
# NOTE: Requires 'dat' object from Holger's analysis

# LOAD PACKAGES --------------------
library(tidyverse)


setwd("~/Dropbox/Current projects/kern-pietryka-crabtree/data/")
load("Experiment 3 results.RData")


# CHOOSE COLOR SCHEME ------------
pal <- c(
  "Fitchburg State" = "#00563F",
  "Harvard" = "#C90016",
  "No banner" = "grey50"
  )

##  TIDY DATA ------------------

df <- data_frame(
  Tr = dat$Tr,
  knowtime = dat$knowtime,
  Flesch = dat$Flesch,
  essay.time = dat$essay.time,
  char.no = dat$char.no
)  %>%
  mutate(logo = recode(Tr,
                       `1` = "No banner",
                       `2` = "Harvard",
                       `3` = "Fitchburg State")
  )

# data frame showing mean for each item by treatment group
df_means <- df  %>%
  group_by(logo)  %>%
  mutate_at(vars(knowtime, Flesch, essay.time, char.no),
            funs(ifelse(is.infinite(.), NA, .)))  %>%
  summarise_at(vars(knowtime, Flesch, essay.time, char.no), mean, na.rm = TRUE)

# data for annotation
text_df <- df_means  %>%
  filter(logo == "No banner")

##  plotting knowtime ------------------

ggplot(df, aes(x = knowtime, fill = logo)) +
  facet_wrap(~ logo, ncol = 1, as.table = FALSE) +
  geom_density()+
  geom_vline(data = df_means, aes(xintercept = knowtime)) +
  theme_minimal(base_size = 14) +
  theme(legend.position = "none",
        panel.grid.minor = element_blank(),
        strip.text = element_text(face = "bold", size = rel(1.0))) +
  xlab("Response time (seconds)") +
  ylab("Density") +
  scale_fill_manual(values = pal) +
  geom_text(data = text_df,
            inherit.aes = FALSE,
            aes(x = knowtime),
            y = Inf,
            label = "mean",
            hjust = -0.1,
            vjust = 1.5) +
  scale_x_log10(labels = scales::comma) +
  labs(caption = "x axis is on a log-10 scale") +
  theme(plot.margin = margin(r = 12, unit = "pt"))



ggsave("plots/Study3-knowtime.pdf", height = 6, width = 6)

##  plotting essay.time  ------------------

ggplot(df, aes(x = essay.time, fill = logo)) +
  facet_wrap(~ logo, ncol = 1, as.table = FALSE) +
  geom_density()+
  geom_vline(data = df_means, aes(xintercept = essay.time)) +
  theme_minimal(base_size = 14) +
  theme(legend.position = "none",
        panel.grid.minor = element_blank(),
        strip.text = element_text(face = "bold", size = rel(1.0))) +
  xlab("Response time (seconds)") +
  ylab("Density") +
  scale_fill_manual(values = pal) +
  geom_text(data = text_df,
            inherit.aes = FALSE,
            aes(x = essay.time),
            y = Inf,
            label = "mean",
            hjust = -0.1,
            vjust = 1.5) +
  scale_x_log10(labels = scales::comma) +
  labs(caption = "x axis is on a log-10 scale")


ggsave("plots/Study3-essaytime.pdf", height = 6, width = 6)


##  plotting Flesch ------------------

ggplot(df, aes(x = Flesch, fill = logo)) +
  facet_wrap(~ logo, ncol = 1, as.table = FALSE) +
  geom_density()+
  geom_vline(data = df_means, aes(xintercept = Flesch)) +
  theme_minimal(base_size = 14) +
  theme(legend.position = "none",
        panel.grid.minor = element_blank(),
        strip.text = element_text(face = "bold", size = rel(1.0))) +
  xlab("Flesch score (higher scores are easier to read)") +
  ylab("Density")  +
  scale_y_continuous(breaks = scales::pretty_breaks(3)) +
  scale_fill_manual(values = pal) +
  geom_text(data = text_df,
            inherit.aes = FALSE,
            aes(x = Flesch),
            y = Inf,
            label = "mean",
            hjust = 1,
            vjust = 1.5)

ggsave("plots/Study3-Flesch.pdf", height = 6, width = 6)

##  plotting number of characters  ------------------

ggplot(df, aes(x = char.no, fill = logo, group = logo)) +
  facet_wrap(~ logo, ncol = 1, as.table = FALSE) +
  geom_density()+
  geom_vline(data = df_means, aes(xintercept = char.no)) +
  theme_minimal(base_size = 14) +
  theme(legend.position = "none",
        panel.grid.minor = element_blank(),
        strip.text = element_text(face = "bold", size = rel(1.0))) +
  xlab("Number of characters") +
  ylab("Density")  +
  scale_fill_manual(values = pal) +
  geom_text(data = text_df,
            inherit.aes = FALSE,
            aes(x = char.no),
            y = Inf,
            label = "mean",
            hjust = -0.1,
            vjust = 0.9) +
  scale_x_log10() +
  labs(caption = "x axis is on a log-10 scale")


ggsave("plots/Study3-charno.pdf", height = 6, width = 6)
